import {defineStore} from 'pinia'
import request from '@/utils/request'
import {ApiRes,BannerItem,GoodsItem,HotGoods,Brand} from '@/types/data'

const useHomeStore  = defineStore("home",{
    state(){
        return {
            bannerList:[] as BannerItem[],
            newGoodsList: [] as GoodsItem[],
            hotGoodsList: [] as HotGoods[],
            brandList: [] as Brand[]
        }
    },
    actions:{
        // 获取首页轮播图数据
        async getBannerList(){
            const res = await request.get<ApiRes<BannerItem[]>>('/home/banner')
            this.bannerList = res.data.result 
        },

        // 获取新鲜好物的数据
        async getNewGoodsList(){
            setTimeout(async ()=>{
                const res = await request.get<ApiRes<GoodsItem[]>>('/home/new')
                this.newGoodsList = res.data.result
            },3000)

            // const res = await request.get<ApiRes<GoodsItem[]>>('/home/new')
            // this.newGoodsList = res.data.result
        },

        // 获取人气推荐数据
        async getHotGoodsList(){
            setTimeout(async ()=>{
                const res = await request.get<ApiRes<HotGoods[]>>('/home/hot')
                this.hotGoodsList = res.data.result
            },3000)
            // const res = await request.get<ApiRes<HotGoods[]>>('/home/hot')
            // this.hotGoodsList = res.data.result
        },

        async getBrandList(){
            setTimeout(async ()=>{
                const res = await request.get<ApiRes<Brand[]>>('/home/brand')
                this.brandList = res.data.result
            },3000)
           
        }
    },
    getters:{}
})


export default useHomeStore